package com.baosight.sczx.zm.cl.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cl.ItemAssciation;
import com.baosight.sczx.common.zm.cInterface.cl.SurveyPonit;
import com.baosight.sczx.zm.cl.domain.ZMCLPointAssociation;
/**
 * <p>新增巡检点</p>
 * <p>Title: ServiceZMCL92.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2011-4-1 高强  Create
 *</pre>
 */
public class ServiceZMCL92 extends ServiceEPBase {
	private String fdParentId = "";
	private  static List<String> msgList ;
    private EiInfo Info;
	/**新增巡检点 进入初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-1 高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/** 新增巡检点
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-1 高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		EiBlock idBlock = inInfo.getBlock("myInsert");
		StaticTools.setQueryParameters(inInfo, "fdSecuritypathId", fdParentId, true);//添加查询条件
		EiInfo Info = super.query(inInfo, "ZMCLPointAssociation.getMaxPathSerial", new ZMCLPointAssociation());//获得最大的编号
		String str = "";
		if(Info.getBlock("result").getCellStr(0, "fdPathSerial") == null){
			str = "0";
		}else
		{
			str = Info.getBlock("result").getCellStr(0, "fdPathSerial").toString();
		}
		boolean validateValue = true;
		for(int i=0;i<idBlock.getRowCount();i++)
		{
			idBlock.setCell(i, "fdKeyId", TablePk.getInstance().getNextValue( "T_CL_POINTASSOCIATION" ));
			idBlock.setCell(i, "fdSecuritypathId", fdParentId);
			idBlock.setCell(i, "fdPathSerial", Integer.toString(Integer.parseInt(str)+1+i));
			idBlock.setCell(i, "fdStatus", "未保存");
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdSecuritypathId", fdParentId);
			params.put("fdSurveypointId", idBlock.getCell(i, "fdSurveypointId").toString());
			boolean validate= validate(inInfo,params);//新增合法性校验
			validateValue= validateValue && validate;
		}
		if(!validateValue) {
			inInfo.setStatus(-1);
			StringBuffer msg = new StringBuffer();//组合提示语句
			msg.append("选中的巡检点");
			for(int j=0;j<msgList.size();j++ )
			{
				if(j==msgList.size()-1)
				{
					msg.append(msgList.get(j));
					break;
				}
				msg.append(msgList.get(j)+"、");
				
			}
			msg.append("已经添加，请重新选择！");
			inInfo.setMsg(msg.toString());
			msgList.clear();
			return inInfo;
		}
		EiInfo outInfo = super.update(inInfo,"ZMCLPointAssociation.insert", "myInsert");
		return outInfo;
	}

	/** 获得目前巡检路线下的所有巡检点
	 * <p>Function: queryPoint</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL06</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-1 高强
	 * </p>
	 *
	*/
	public EiInfo query(EiInfo inInfo) {
		fdParentId = inInfo.getString("fdParentId");
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdStatus", "在用", true);//添加查询条件
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.offsetStr, 0);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.limitStr, 1000);
		inInfo.getBlock(EiConstant.resultBlock).set(EiConstant.isCountFlag, true);
		EiInfo pointInfo = super.query(inInfo, "ZMCLPointAssociation.query", new ZMCLPointAssociation());
		if(pointInfo.getBlock("result").getRowCount()!=0)
		{
			if(pointInfo.getBlock("result").getRowCount()>1)
			{
				Info = pointInfo;
			}
		}
		return pointInfo;
	}
	
	/** 获得所有的巡检点
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-1 高强
	 * </p>
	 *
	*/
	public EiInfo queryAllSurveyPoint(EiInfo inInfo) {
		EiInfo outInfo = getSurveyPointInfo(SurveyPonit.querySurveypoint(inInfo));
		return outInfo;
	}
	
	/**
	 * 获得巡检点信息
	 * <p>Function: getSurveyPointInfo</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7   高强
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private EiInfo getSurveyPointInfo(EiInfo info)
	{
		EiBlock resultBlock = info.getBlock(EiConstant.resultBlock);
		for(int i=0; i<resultBlock.getRowCount(); i++)
		{
			//信息内容为巡检点名称和巡检点位置
			String surveyPointInfo = resultBlock.getRow(i).get("fdSurveypointName").toString()+"("+resultBlock.getRow(i).get("fdPosition").toString()+")";
			resultBlock.getRow(i).put("fdSurveyPointInfo", surveyPointInfo);
		}
		return info;
	}

	/**
	 * 检查是否添加重复的巡检点存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7下午01:32:59 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query("ZMCLPointAssociation.query", params);
		if(result!=null && result.size() > 0) {
			ZMCLPointAssociation data = (ZMCLPointAssociation)result.get(0);
			if(msgList == null) {
				msgList =  new ArrayList<String>();
			}
			msgList.add(data.getFdSurveypointName());//获得重复了的巡检点名称
			return false;
		}
		return true;
	}
	
	/**
	 * 删除 巡检路线下的巡检点
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28              高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo)
	{
		if(!ItemAssciation.deleteItem(inInfo))//先删除巡检点下的安全检查项目
		{
			inInfo.setStatus(-1);
			inInfo.setMsg("删除巡检点下的安全检查项目失败！");
			return inInfo;
		}
		EiInfo _temp = super.delete(inInfo, "ZMCLPointAssociation.delete");
		EiInfo outInfo = query(_temp);
		return saveNewOrder(outInfo);
	}
	
	/**
	 * 设置新的巡检顺序
	 * <p>Function: saveNewOrder</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-7             高强  
	 * </p>
	 *
	*/
	public EiInfo saveNewOrder(EiInfo inInfo)
	{
		EiBlock gridBlock = inInfo.getBlock("result");
		if (null != gridBlock) {
			for (int i = 0; i < gridBlock.getRowCount(); i++) {
				gridBlock.setCell(i, "fdPathSerial", Integer.toString(i + 1));
			}
		}
		return updateOrder(inInfo);
	}
	
	/**
	 * 更新巡检顺序
	 * <p>Function: updateOrder</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-7             高强  
	 * </p>
	 *
	*/
	
	public EiInfo updateOrder(EiInfo inInfo)
	{
		EiInfo outInfo = super.update(inInfo,"ZMCLPointAssociation.update", "result");
	    return outInfo;
    }
	
	/**
	 * 巡检顺序 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-4-7             高强  
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		String curSerial = inInfo.getBlock("result").getCell(0, "fdPathSerial").toString();//获得修改后的顺序
		String beforeSerial = inInfo.getString("beforeSerial").toString();
		EiBlock block = Info.getBlock("result");//获得修改前保存的顺序block
		if(Integer.parseInt(beforeSerial)>Integer.parseInt(curSerial))//新顺序和原来的顺序比较
		{
			for (int i = Integer.parseInt(curSerial)-1; i < Info.getBlock("result").getRowCount(); i++) {
				if(i<=Integer.parseInt(beforeSerial) -1)
				{
					if(i == Integer.parseInt(beforeSerial) -1)//找到要修改的值得位置
					{
						block.setCell(i, "fdPathSerial", curSerial);//修改为新的顺序
					}else
					{
						block.setCell(i, "fdPathSerial", Integer.toString(i + 2));
					}
				}else
				{
					block.setCell(i, "fdPathSerial", Integer.toString(i+1));
				}
			}
		}else if(Integer.parseInt(beforeSerial)<Integer.parseInt(curSerial))
		{
			for (int i = Integer.parseInt(beforeSerial)-1; i < Info.getBlock("result").getRowCount(); i++) {
				if(i<Integer.parseInt(curSerial) -1)
				{
					if(i==Integer.parseInt(beforeSerial) -1)
					{
						if(Integer.parseInt(curSerial)>Info.getBlock("result").getRowCount())//如果输入的顺序值大于数据条数最大值，则以数据条数为准
						{
							block.setCell(i, "fdPathSerial", Info.getBlock("result").getRowCount());
						}else
						{
							block.setCell(i, "fdPathSerial", curSerial);
						}
					}else
					{
						block.setCell(i, "fdPathSerial", Integer.toString(i));
					}
				}else{
					if(i >= Integer.parseInt(curSerial))
					{
						block.setCell(i, "fdPathSerial", Integer.toString(i+1));
					}else
					{
						block.setCell(i, "fdPathSerial", Integer.toString(i));
					}
				}
			}
		}else
		{
			return inInfo;
		}
		EiInfo outInfo = super.update(Info, "ZMCLPointAssociation.update");
		return outInfo;
	}
}
